
-module(lib_misc).
-compile(export_all).
% -export ([qsort/1, pythag/1]).


qsort([]) -> [];
qsort([P | T]) ->
    qsort([X || X <-T, X < P]) ++ [P] ++ qsort([X || X <-T, X >= P]).


pythag(N) ->
    [ {A,B,C} || A <- lists:seq(1,N),
                 B <- lists:seq(1,N),
                 C <- lists:seq(1,N),
                 A + B + C <= N,
                 A*A + B*B =:= C*C
    ].


perms([]) -> [[]];
perms(L) -> [[H|T] || H <- L, T <- perms(L -- [H])].


odds_and_evens_acc(L) -> odds_and_evens_acc(L, [], []).

odds_and_evens_acc([H|T], Odds, Evens) ->
    case (H rem 2) of
        1 -> odds_and_evens_acc(T, [H|Odds], Evens);
        0 -> odds_and_evens_acc(T, Odds, [H|Evens])
    end;
odds_and_evens_acc([], Odds, Evens) ->
    {lists:reverse(Odds), lists:reverse(Evens)}.


unconsult(File, L) ->
    {ok, S} = file:open(File, write),
    list:foreach(fun(X) -> io:format(S, "~p.~n", [X]) end, L),
    file:close(S).

